<template>
    <el-dialog
            :title="!dataForm.supplierId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="供应商ID" prop="supplierId">
                        <el-input v-model.number="dataForm.supplierId" placeholder="供应商ID"></el-input>
                </el-form-item>
                <el-form-item label="供应商名称" prop="supplierName">
                        <el-input v-model="dataForm.supplierName" placeholder="供应商名称"></el-input>
                </el-form-item>
                <el-form-item label="信用评级" prop="creditRating">
                        <el-input v-model="dataForm.creditRating" placeholder="信用评级"></el-input>
                </el-form-item>
                <el-form-item label="付款条件" prop="paymentTerms">
                        <el-input v-model="dataForm.paymentTerms" placeholder="付款条件"></el-input>
                </el-form-item>
                <el-form-item label="联系人" prop="contactPerson">
                        <el-input v-model="dataForm.contactPerson" placeholder="联系人"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" prop="phone">
                        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
                </el-form-item>
                <el-form-item label="电子邮箱" prop="email">
                        <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            supplierId: null,
            supplierName: null,
            creditRating: null,
            paymentTerms: null,
            contactPerson: null,
            phone: null,
            email: null
          },
          dataRule: {
            supplierId: [
                        { required: true, message: '供应商ID不能为空', trigger: 'blur' }
            ],
            supplierName: [
                        { required: true, message: '供应商名称不能为空', trigger: 'blur' }
            ],
            creditRating: [
                        { required: true, message: '信用评级不能为空', trigger: 'blur' }
            ],
            paymentTerms: [
                        { required: true, message: '付款条件不能为空', trigger: 'blur' }
            ],
            contactPerson: [
                        { required: true, message: '联系人不能为空', trigger: 'blur' }
            ],
            phone: [
                        { required: true, message: '联系电话不能为空', trigger: 'blur' }
            ],
            email: [
                        { required: true, message: '电子邮箱不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.supplierId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.supplierId = id || 0
            if (this.dataForm.supplierId) {
              this.$http({
                url: this.$http.adornUrl(`/app/suppliers/info/${this.dataForm.supplierId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appsuppliers
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/suppliers/${!this.dataForm.supplierId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>